package com.lsh.oms.worker.service.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lsh.oms.core.enums.TaskStatus;
import com.lsh.oms.core.enums.TaskType;
import com.lsh.oms.core.model.http.HttpMessage;
import com.lsh.oms.core.model.order.OrderSignHead;
import com.lsh.oms.core.model.po.OmsTask;
import com.lsh.oms.core.proxy.service.MisProxy;
import com.lsh.oms.core.service.other.SignOrderService;
import com.lsh.oms.worker.service.base.OmsSimpleElasticJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

/**
 * @author zhangbiao
 * @date 18/5/16
 * 签收单数据push同步
 */
@Slf4j
@Component
public class OrderReceipt2AccountTask extends OmsSimpleElasticJob {

    @Autowired
    private SignOrderService signOrderService;

    @Autowired
    private MisProxy MisProxy;

    @Override
    protected Integer getFetchSize() {
        return 100;
    }

    @Override
    protected TaskType getTaskType() {
        return TaskType.RECEIPT_TO_ACCOUNT;
    }

    @Override
    protected List<Integer> dealTaskStatus() {

        return Arrays.asList(TaskStatus.TASK_UN_DEAL.getCode(), TaskStatus.TASK_DEAL_FAIL.getCode());
    }

    @Override
    protected int process(OmsTask omsTask) {

        OrderSignHead orderSignHead = signOrderService.getSignHead(Long.parseLong(omsTask.getRefCode()), false);
        orderSignHead.getOrderId();
        try {
            HttpMessage httpMessage = MisProxy.notifyReceipt2Account(orderSignHead.getOrderId(), orderSignHead.getVenderId());
            log.info("{} -【通知 mis 订单签收】:{}", omsTask.getRefCode(), httpMessage.getMessage());

            if (StringUtils.isBlank(httpMessage.getMessage())) {
                log.info(omsTask.getRefCode() + "【通知 mis 返仓结果】【失败】");
                return -1;
            }

            JSONObject messageJsonVo = JSON.parseObject(httpMessage.getMessage());
            if (messageJsonVo.getInteger("ret") == 0) {
                return 1;
            } else {
                return -1;
            }

        } catch (Exception e) {
            log.error("签收到账通知mis失败", e);
            return -1;
        }

    }

}
